import {} from './actionCreators'
import { fromJS } from 'immutable'
import {
  CHANGE_HOME_DATA,
  GET_MORE_ARTICLE_LIST,
  CHANGE_SCROLL_SHOW
} from './actionCreators'

const defaultState = fromJS({
  topicList: [],
  articleList: [],
  recommendList: [],
  writerList: [],
  articlePage: 1,
  showScroll: false
})

// 将 switch 语句中的部分代码拆分出来封装，使代码看起来更精简
const changeHomeData = (state, action) => {
  return state.merge({
    topicList: fromJS(action.topicList),
    articleList: fromJS(action.articleList),
    recommendList: fromJS(action.recommendList),
    writerList: fromJS(action.writerList)
  })
}

const getMoreArticleList = (state, action) => {
  return state.merge({
    articleList: state.get('articleList').concat(action.list),
    articlePage: action.nextPage
  })
}

export default (state = defaultState, action) => {
  switch (action.type) {
    case CHANGE_HOME_DATA:
      return changeHomeData(state, action)
    case GET_MORE_ARTICLE_LIST:
      return getMoreArticleList(state, action)
    case CHANGE_SCROLL_SHOW:
      return state.set('showScroll', action.isShow)
    default:
      return state
  }
}
